﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class SettlementInputAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
              

                //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
                this.txtDateReckoning.Attributes["readonly"] = "true";
                this.txtDateReckoning.Date = System.DateTime.Now;
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                this.lblManagerTrueName.Text = currentUser.TrueName;
                this.lblManagerUserName.Text = currentUser.UserName;
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.lblInfoTo.Text = "本公司";
                this.BindAccountID();
                this.ShowCurrentBalance(this.ddlAccountID, this.lblCurrentBalance);
                this.BindTypeID();
            }
        }
        private void BindTypeID()
        {
          //  UART.BLL.Sales.SettlementType bll = new UART.BLL.Sales.SettlementType();
            //把客户付款、转账的选项去掉
            var ds = mk.l.Sales_SettlementType.Where(y => y.Flag == "2" & y.TypeID > 4).ToList();
            //bll.GetList(" Flag='2' and TypeID>4");
            this.ddlTypeID.DataSource = ds;
            this.ddlTypeID.DataTextField = "Name";
            this.ddlTypeID.DataValueField = "TypeID";
            this.ddlTypeID.DataBind();
        }

        /// <summary>
        /// Binds the account ID.绑定账户列表
        /// </summary>
        private void BindAccountID()
        {
          //  UART.BLL.Sales.Account bll = new UART.BLL.Sales.Account();
      var ds = mk.l.Sales_Account.Where(y=>y.RootDepartmentID.ToString()==this.lblRootDepartmentID.Text).ToList();
            this.ddlAccountID.DataSource = ds;
            this.ddlAccountID.DataTextField = "Name";
            this.ddlAccountID.DataValueField = "AccountID";
            this.ddlAccountID.DataBind();
        }

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
               // UART.BLL.Sales.SettlementDetail bll = new UART.BLL.Sales.SettlementDetail();
             Sales_SettlementDetail   m = new Sales_SettlementDetail();
                m.AccountID = int.Parse(this.ddlAccountID.SelectedValue);
                m.DateReckoning = Convert.ToDateTime(this.txtDateReckoning.Date);
                m.Flag = "2";
                m.InfoFrom = mk.InputText(this.txtInfoFrom.Text, 300);
                m.InfoTo = this.lblInfoTo.Text;
                m.Manager = this.lblManagerUserName.Text;
                m.ModifiedDate = System.DateTime.Now;
                m.Notes = mk.InputText(this.txtNotes.Text, 200);
                m.PurchaseOrderID = 0;
                m.RootDepartmentID = int.Parse(this.lblRootDepartmentID.Text);
                m.SalesOrderID = 0;
                //金额设置为正值
                m.SettlementAmount = decimal.Parse(this.txtSettlementAmount.Text);
                m.TypeID = int.Parse(this.ddlTypeID.SelectedValue);
                try
                {
                    //bll.Add(m);
                    mk.l.Sales_SettlementDetail.InsertOnSubmit(m);
                    mk.l.SubmitChanges();
                    //更新账户的信息
                var bllAccount =mk.l.Sales_Account.ToList();
                  Sales_Account mAccount = bllAccount.Where(h=>h.AccountID==int.Parse(this.ddlAccountID.SelectedValue)).FirstOrDefault();
                    mAccount.Balance += decimal.Parse(this.txtSettlementAmount.Text);
                  //  bllAccount.Update(mAccount);
                    mk.l.SubmitChanges();
                    //提交事务
                   // ContextUtil.SetComplete();
                   mk.Alert("添加成功！", Request.Url.ToString());
                }
                catch (Exception ex)
                {
                    //撤销事务
                 //   ContextUtil.SetAbort();
                   mk.Alert("添加失败！错误信息：" + ex.Message, Request.Url.ToString());
                }
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the ddlAccountID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ddlAccountID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowCurrentBalance(this.ddlAccountID, this.lblCurrentBalance);
        }

        /// <summary>
        /// Shows the current balance.
        /// </summary>
        /// <param name="ddlAccountID">The DDL account ID.</param>
        /// <param name="lblCurrentBalance">The LBL current balance.</param>
        private void ShowCurrentBalance(DropDownList ddlAccountID, Label lblCurrentBalance)
        {
            if (ddlAccountID.SelectedValue != "")
            {
              var bll = mk.l.Sales_Account.ToList();
                Sales_Account m = bll.Where(h=>h.AccountID==int.Parse(ddlAccountID.SelectedValue)).FirstOrDefault();
                lblCurrentBalance.Text = m.Balance.ToString();
            }
        }
    }
}
